import { Node } from "cc";
import { IVector2, Maths, Move, Sets, Vector2, _logic, _timer, ccclass } from "../../../main/script/Main";
import { EEntityType, ELogicDataState, ILogicData } from "../../../main/script/module/define/LogicDefine";
import { Item } from "../Item";
import { Entity } from "./Entity";
import { TipEntity } from "./TipEntity";

const _v2T = new Vector2()

@ccclass("CubeEntity")
export class CubeEntity extends Entity {

    public entityType = EEntityType.Cube
    public isPut = true
    public isUpdateStateMackine = true
    public isDestroy = false
    public isMoving() { return false }

    private cADragTip: Node = null!

    public data: ILogicData = null!
    private _move = new Move()

    public animComplete: () => void = null!

    private _isResetAnim = false
    protected _curAnimCount = 0
    protected _toAnimCount = 0
    private _tipAnimEntity: TipEntity = null!

    public _isLerpAnim = false
    public _lerpPos = new Vector2()
    private _lastChildIndex = -1
    private _lerpRatio = 0.2

    public get item() { return this.getCacheComponent(Item) }

    protected onStateEnterLoad(): void {
        super.onStateEnterLoad()
        this.setScaleNum(_logic.scaleRatio, false)

        this._move.init(this.node, false, 66 * 60)


        let _pos: IVector2 = !this.animComplete ? this.data.rect.center : _logic.ten.data.datas[_logic.animIndex].rect.center
        // if (!this.animComplete)
        //     pos = this.data.rect.center
        // else
        //     pos = _logic.ten.data.datas[_logic.animIndex].rect.center
        this.setPositionXY(_pos, false)
    }

    protected onStateEnterRun(): void {
        super.onStateEnterRun()
        if (this.animComplete) {
            this._move.setRunData(1, data => {
                Vector2.set(data.target, this.data.rect.center)
                data.speed = 3600
            })
            this._move.run(this.animComplete)
        }
        this.updateState()
    }


    protected onStateUpdateRun(): void {
        super.onStateUpdateRun()
        if (this._isLerpAnim) {
            Vector2.lerp(_v2T, this.node.position, this._lerpPos, this._lerpRatio * _timer.dtSecond)
            this.setPositionXY(_v2T, false)

            if (Vector2.distanceSqr(_v2T, this.getCurPos()) < 1) {
                if (this._lastChildIndex != -1) {
                    this.node.setSiblingIndex(this._lastChildIndex)
                }
                this._lastChildIndex = -1
            }
        } else {
            this._move.onUpdate()
        }

        if (this._isResetAnim) {
            this._curAnimCount++
            if (this._curAnimCount >= this._toAnimCount) {
                this._toAnimCount += Maths.minToMax(5, 20)
                this.item.setConfigFrame(Sets.random(_logic.ten.cardIndexs))
            }
        }
    }

    public updateState() {
        switch (this.data.state) {
            case ELogicDataState.none:
                this.item.setOpacity(0)
                break
            case ELogicDataState.back:
                this.item.setOpacity(1)
                this.item.setIconState(false)
                break
            case ELogicDataState.card:
                this.item.setOpacity(1)
                this.item.setConfigFrame(this.data.configId)
                this.item.setIconState(true)
                break
            case ELogicDataState.cardStatic:
                this.item.setOpacity(.7)
                this.item.setConfigFrame(this.data.configId)
                this.item.setIconState(true)
                break
        }
        this.setDragTip(false)
    }

    public setDragTip(_value: boolean) {
        this.cADragTip.active = _value
    }

    public getCurPos() {
        return this.data.rect.center
    }

    public resetItemPos(lerp = true, _lerpRatio = .2) {
        this._lerpPos.set(this.getCurPos())
        this._lerpRatio = _lerpRatio
        if (!lerp)
            this.setPositionXY(this._lerpPos, false)
    }

    public getConfigId() {
        return this.data.configId
    }

    public setItemPos(pos: IVector2) {
        this._lerpPos.set(pos)
        this._lerpRatio = .6
        if (this._lastChildIndex == -1) {
            this._lastChildIndex = this.node.getSiblingIndex()
            this.node.setSiblingIndex(this.node.parent.children.length - 1)
        }
    }

    protected onStateEnterExit(arg?: string): void {
        this._move.clear()
        this.tipAnimStop()
        this._isLerpAnim = false
        this._lastChildIndex = -1
        this.setDragTip(false)
        super.onStateEnterExit()
    }

    public startScaleAnim(duration: number, complete?: () => void, randomScaleX = true) {
        if (_logic.isTenGameType)
            _logic.ten.changeState(this.data, ELogicDataState.card)
        this.item.scaleAnim(duration, complete, true, randomScaleX ? "random" : "x")
    }

    public startMoveAnim(setIndex: boolean, complete: () => void) {
        if (setIndex)
            this.node.setSiblingIndex(this.node.parent.children.length - 1)

        this._move.setRunData(1, data => {
            Vector2.set(data.target, this.data.rect.center)
            data.speed = 600
        })
        this._move.run(() => {
            if (setIndex)
                this.node.setSiblingIndex(0)

            complete()
        })
    }

    public playResetAnim() {
        this._curAnimCount = 0
        this._toAnimCount = 0
        this._isResetAnim = true
    }

    public stopResetAnim() {
        this._curAnimCount = 0
        this._toAnimCount = 0
        this._isResetAnim = false

        this.item.setConfigFrame(this.data.configId)
    }

    public remove(_isLeft: boolean, _complete?: () => void) {
        let entity = this.entityMgr.createCubeAnim()
        entity.startAnim(
            this.data.configId,
            this.node.position,
            _isLeft,
            _complete
        )
    }


    public tipAnim() {
        this._tipAnimEntity = this.scene.entityMgr.createTip(this)
    }

    public tipAnimStop() {
        if (this._tipAnimEntity) {
            this.scene.entityMgr.remove(this._tipAnimEntity)
            this._tipAnimEntity = null!
        }

    }


}